#include <gtk/gtk.h>
#include <string.h>
#include "common.h"

void do_clicked(GtkWidget *w, gpointer data)
{
    g_message("message: %c", *(char *)data);
}

int main(int argc,char *argv[])
{
    GtkWidget *window;
    GtkWidget *table;
    GtkWidget *displayLabel;
    GtkWidget *button;
    char *values[16] = { "7", "8", "9", "/", "4", "5", "6", "*", "1", "2", "3", "-", "0", ".", "=", "+"};
    int i, j, pos;

    GdkColor  color_black_ = {0x0, 0xffff, 0xffff, 0xffff};

    gtk_init(&argc,&argv);

    /* window */
    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title(GTK_WINDOW(window), TITLE);
    gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
    gtk_window_set_default_size(GTK_WINDOW(window), 250, 200);
    gtk_widget_modify_bg(window, GTK_STATE_NORMAL, &color_black_); /*设置为黑色背景*/


    /* label */
    displayLabel = gtk_label_new("Hello world");

    /* table */
    table = gtk_table_new(5, 4, TRUE);
    gtk_table_set_row_spacings(GTK_TABLE(table), 2);
    gtk_table_set_col_spacings(GTK_TABLE(table), 2);
    gtk_container_add(GTK_CONTAINER(window), table);

    gtk_table_attach_defaults(GTK_TABLE(table), displayLabel, 0, 4, 0, 1);
    for (i = 1; i < 5; ++i) {
        for (j = 0; j < 4; ++j) {
            button = gtk_button_new_with_label(values[pos]);
            gtk_table_attach_defaults(GTK_TABLE(table), button, j, j+1, i, i+1);
            g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(do_clicked), values[pos]);
            ++pos;
        }
    }

    /* set signal */
    g_signal_connect_swapped(G_OBJECT(window),"destroy",G_CALLBACK(gtk_main_quit),NULL);


    /* make sure that everything, window and label, are visible */
    gtk_widget_show_all(window);

    /* start the main loop, and let it rest until the application is closed */
    gtk_main();

    return 0;
}
